/**
 * Really, really in progress
 **/

package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	import org.flixel.*;

	public class ArrowBin extends FlxGroup
	{
		protected static const BIN_SIZE:int = GameState.CELL_SIZE;
		protected static const BIN_COLOR:uint = 0xCCCCCC;
		protected static const LINE_COLOR:uint = 0x444444;
		protected var numCells:int;
		protected var numRows:int;
		protected var numCols:int;
		protected var cellMap:FlxGroup;
		
		private var countArrow:Array;
		private var background:FlxSprite;
		private var textGroup:Array;
		
		public function ArrowBin(_x:int, _y:int, _rows:int, _cols:int, _count:Array)
		{
			super();
			numCells = _rows * _cols;
			numRows = _rows;
			numCols = _cols * 2;
			countArrow = _count;
			
			background = new FlxSprite(_x,_y);
			
			// MUST make graphic before drawing lines
			// otherwise nothing but a tiny flixel icon will appear (??)
			background.makeGraphic(numCols * BIN_SIZE, _rows * BIN_SIZE, 0x000000,true);
			background.drawRect(0, 0, numCols * BIN_SIZE, _rows * BIN_SIZE, BIN_COLOR);

			// draw out row lines
			for (var i:int = 0; i <= numRows; i++) {
				background.drawLine(0 , i * BIN_SIZE , numCols * BIN_SIZE, i * BIN_SIZE , LINE_COLOR, 5);
			}
			
			// draw out cols lines
			for (var j:int = 0; j <= numCols; j++) {
				background.drawLine(j * BIN_SIZE , 0 , j * BIN_SIZE , numRows * BIN_SIZE , LINE_COLOR, 5);
			}
			this.add(background);
			
			//set up the arrows:
			for (var c:int = 0; c < countArrow.length; c++){
				if (countArrow[c] >= 1){
					var arr:Arrow = new Arrow(_x,_y+c*BIN_SIZE,c);
					this.add(arr);
				}
			}			
			
			textGroup = new Array();
			for (var a:int = 0; a < countArrow.length; a++){
				var num:FlxText = new FlxText(_x + BIN_SIZE,_y+a*BIN_SIZE,BIN_SIZE,countArrow[a]);
				num.setFormat(null,20,0x000000,"center");
				num.y = num.y + (BIN_SIZE - num.height)/2;
				textGroup.push(num);
				this.add(num);
			}
			
			active = true;
		}
		
		
		/**
		 * Detects if an arrow is clicked on or not, if yes, place on
		 * the position of the player
		 */
		public function dispatchArrow(index:int):Arrow{
			for (var i:int = 0; i < members.length; i++) {
				if (members[i] is Arrow){
					var arrow:Arrow = members[i];
					if (arrow.isClicked()){
						this.remove(arrow);
						//var index:int = GameState.getIndexFromPoint(player.x+player.width/2, player.y+player.height/2);
						var p:Point = GameState.getPointFromIndex(index);
						arrow.x = p.x;
						arrow.y = p.y;
						countArrow[arrow.getOrientation()]--;
						if (countArrow[arrow.getOrientation()] > 0){
							var arr:Arrow = new Arrow(background.x,background.y+arrow.getOrientation()*BIN_SIZE,arrow.getOrientation());
							this.add(arr);
						}
						return arrow;
					}
				}
			}
			return null;
		}
		
		public override function update():void {
			super.update();
			for (var i:int = 0; i < textGroup.length; i++){
				var num:FlxText =textGroup[i] as FlxText;
				num.text = countArrow[i];
			}
		}
		
		public function overlapsPoint(X:Number,Y:Number):Boolean{
			return background.overlapsPoint(X,Y);
		}
		
		public function reclaimArrow(arrowOut:FlxGroup, arrowMap:Dictionary):FlxGroup{
			for (var i:int = 0; i < arrowOut.length; i++){
				if (arrowOut.members[i] is Arrow){
					var arrow:Arrow = arrowOut.members[i] as Arrow;
					if (arrow.isClicked()){
						countArrow[arrow.getOrientation()]++;
						if(countArrow[arrow.getOrientation()] == 1){
							var arr:Arrow = new Arrow(background.x,background.y+arrow.getOrientation()*46,arrow.getOrientation());
							this.add(arr);
						}
						var index:int = GameState.getIndexFromPoint(arrow.x,arrow.y);
						arrowMap[index] = null;
						arrowOut.remove(arrow,true);
						arrow.destroy();
					}
				}
			}
			return arrowOut;
		}

	}
}